﻿using Bookify.Application.Abstractions.Email;
using Bookify.Domain.Bookings;
using Bookify.Domain.Bookings.Events;
using Bookify.Domain.Users;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bookify.Application.Bookings.ReserverBooking
{
    internal sealed class BookingReservedDomainEventHandler(
        IBookingRepository bookingRepository,
        IUserRepository userRepository,
        IEmailService emailService) : INotificationHandler<BookingReservedDomainEvent>
    {
        public async Task Handle(BookingReservedDomainEvent notification, CancellationToken cancellationToken)
        {
            var booking = await bookingRepository.GetByIdAsync(notification.BookingId,cancellationToken);
            if (booking == null)
            {
                return;
            }

            var user = await userRepository.GetByIdAsync(booking.UserId,cancellationToken);
            if (user == null)
            {
                return;
            }

            await emailService.SendAsync(
                user.Email,
                "房间预定保留",
                "您有10分钟来确定这个预定");
        }
    }
}
